package com.cheng.io.demo;

import java.io.*;

/**
 * 简单文件复制功能，使用FileInputStream和FileOutputStream实现
 */
public class FileCopyByInputOutputStreamImpl {

    public static void main(String[] args) {
        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            inputStream = new FileInputStream("C:\\Users\\ck\\Desktop\\特性.txt");
            outputStream = new FileOutputStream("D:\\code\\java-study\\java-study\\io\\resources\\特性.txt");

            // 每次读取1024b --> 1kb
            byte[] bytes = new byte[1024];
            int readCount = 0;
            while ((readCount = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes,0,readCount);
            }

            // 刷新流中剩余的数据
            outputStream.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            // close stream
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}
